package flex.cc.single.controller;

import flex.cc.PublicUtil;
import flex.cc.common.utils.CommonUtils;
import flex.cc.core.support.BaseController;
import flex.cc.single.dto.vo.SingleOrderVo;
import flex.cc.single.service.AppointmentService;
import flex.cc.store.dto.entity.StoreCouponEntity;
import flex.cc.store.service.StoreCouponService;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wkangle
 * @className SingleCommonController
 * @description 单项服务公共controller
 * @time 2022-03-31 15:49:02
 */
@SuppressWarnings("restriction")
@RestController
@RequestMapping(value = "/vehicleSvr/singleService/wechat")
public class SingleCommonController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SingleCommonController.class);

    @Autowired
    private DictCodeService dictCodeService;
    @Autowired
    private StoreCouponService storeCouponService;
    @Autowired
    private AppointmentService appointmentService;

    @RequestMapping("/init")
    @ResponseBody
    public String init(@RequestBody Map<String,String> params) throws Exception {
        String couponNum = params.get("couponNum");
        String activityId = params.get("activityId");
        Map<String, Object> body = new HashMap<String, Object>();
        //body.put("apiSource", "ZXHY");
        //body.put("sign", "03021F3ABE5DE761F5D50318B291D207");
        //body.put("signType", "MD5");
        //body.put("source", "ZXHY");
        body.put("couponNum", couponNum);
        body.put("activityId", activityId);
        String json = CommonUtils.toJSONString(body);
        byte[] bytes = json.getBytes();
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static void main(String[] args) {
        String params = "eyJhcGlTb3VyY2UiOiJaWEhZIiwiY2FyZF9jb2RlIjoiWlhIWUNGMjAyMjA0MDYxMDMzMDMyMTEiLCJyZWRpcmVjdFVybCI6Imh0dHBzOi8vbWNsdWIuc2lub3NpZy5jb20vY291cG9uTGlzdCIsIm9yZGVySWQiOiI0MzMyNDMzMCIsInNpZ24iOiIwMzAyMUYzQUJFNURFNzYxRjVENTAzMThCMjkxRDIwNyIsInNpZ25UeXBlIjoiTUQ1IiwidGVsZXBob25lIjoiMTU1MTAwNTM0NTEiLCJzb3VyY2UiOiJaWEhZIiwiY3VzdG9tZXJObyI6IiJ9";
        params = "eyJjb3Vwb25OdW0iOiJaWEhZQUVEQjEyNjgzOTczNjIiLCJhY3Rpdml0eUlkIjoiNTUxNyJ9";
        String toString = new String(Base64.getDecoder().decode(params));
        System.out.println("明文==" + toString);
    }

    @PostMapping(value = "/chooseRedirect")
    @ApiOperation(httpMethod = "POST", value = "根据上游链接来的卡券码和服务项目id判断跳转页面")
    public Wrapper<Map<String, String>> chooseRedirect(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("SingleCommonController -- chooseRedirect() ====> {}", reqVo);

        Map<String, Object> params = new HashMap<>();
        params.put("typeCode", "singleActivityName");
        params.put("code", reqVo.get("activityId"));
        DictCodeEntity dictCodeEntity = dictCodeService.getOneByParams(params);
        String type = PublicUtil.isNotEmpty(dictCodeEntity) ? dictCodeEntity.getValue1() : null;

        String couponNum = reqVo.get("couponNum").toString();

        StoreCouponEntity storeCouponEntity = storeCouponService.queryByCouponNum(couponNum);
        if (storeCouponEntity == null) {
            return null;
        }

        SingleOrderVo singleOrderVo = appointmentService.drivingProgress(reqVo);

        Map<String, String> result = new HashMap<>();
        result.put("type", type); // 服务类型：driving-代驾，store-门店洗车
        //result.put("status", storeCouponEntity.getStatus()); // 状态:1-待使用，3-已使用，2-已过期
        result.put("status", "driving".equals(type)
                ? ("1".equals(storeCouponEntity.getStatus() )
                    && (
                        "01".equals(singleOrderVo.getStatus()) ||
                        "02".equals(singleOrderVo.getStatus()) ||
                        "03".equals(singleOrderVo.getStatus()) ||
                        "04".equals(singleOrderVo.getStatus()) ||
                        "06".equals(singleOrderVo.getStatus()))
                    ? "3"
                    : storeCouponEntity.getStatus()) // 代驾类卡券若状态为1且服务单中状态为01-预约成功、02-派单成功、03-司机已到达、04-服务完成、06-取消核销，则表示已预约过，展示进入进展页，展示对应信息
                :  storeCouponEntity.getStatus()); // 状态:1-待使用，3-已使用，2-已过期

        result.put("activityName", storeCouponEntity.getActivityName()); // 服务项目名称
        result.put("effDate", new SimpleDateFormat("yyyy-MM-dd").format(storeCouponEntity.getEffDate())); // 卡券生效时间
        return WrapMapper.ok(result);
    }

    /**
     * @description 根据服务项目id查询服务项目的使用规则【2.2-单项服务】
     * @param activityId 服务项目编号
     * @return
     * @author wkangle
     * @time 2022-04-01 13:37:35
     * @exception/throws
     */
    @GetMapping(value = "/showActivityRule")
    @ApiOperation(httpMethod = "GET", value = "根据服务项目id查询服务项目的使用规则")
    public Wrapper<List<DictCodeEntity>> showActivityRule(@ApiParam(name = "reqVo", value = "请求信息") @RequestParam(name = "activityId") String activityId) {
        logger.info("SingleCommonController -- showActivityRule() ====> {}", activityId);
        Map<String, Object> dictCodeParams = new HashMap<String, Object>();
        dictCodeParams.put("typeCode", "singleActivityRule");
        dictCodeParams.put("value1", activityId);
        List<DictCodeEntity> list = dictCodeService.queryDictCodeListByParams(dictCodeParams);
        return WrapMapper.ok(list);
    }

    /**
     * @description 代驾卡券取消说明【2.2-单项服务】
     * @param activityId 服务项目编号
     * @return
     * @author wkangle
     * @time 2022-04-01 13:37:35
     * @exception/throws
     */
    @GetMapping(value = "/showCouponCancelRule")
    @ApiOperation(httpMethod = "GET", value = "代驾卡券取消说明")
    public Wrapper<List<DictCodeEntity>> showCouponCancelRule(@ApiParam(name = "reqVo", value = "请求信息") @RequestParam(name = "activityId") String activityId) {
        logger.info("SingleCommonController -- showCouponCancelRule() ====> {}", activityId);
        Map<String, Object> dictCodeParams = new HashMap<String, Object>();
        dictCodeParams.put("typeCode", "singleCouponCancelRule");
        dictCodeParams.put("value1", activityId);
        List<DictCodeEntity> list = dictCodeService.queryDictCodeListByParams(dictCodeParams);
        return WrapMapper.ok(list);
    }

    /**
     * @description 代驾卡券取消说明【2.2-单项服务】
     * @param activityId 服务项目编号
     * @return
     * @author wkangle
     * @time 2022-04-01 13:37:35
     * @exception/throws
     */
    @GetMapping(value = "/showCustomerService")
    @ApiOperation(httpMethod = "GET", value = "卡券对应客服二维码")
    public Wrapper<String> showCustomerService(@ApiParam(name = "reqVo", value = "请求信息") @RequestParam(name = "activityId") String activityId) {
        logger.info("SingleCommonController -- showCustomerService() ====> {} ", activityId);
        Map<String, Object> codeParams = new HashMap<String, Object>();
        codeParams.put("typeCode", "singleActivityName");
        codeParams.put("code", activityId);
        DictCodeEntity dictCodeEntity = dictCodeService.getOneByParams(codeParams);
        return WrapMapper.ok(dictCodeEntity.getValue3());
    }

}
